// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Fluent;
using Elastic.Clients.Elasticsearch.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.QueryDsl;

internal sealed partial class DateRangeQueryConverter : JsonConverter<DateRangeQuery>
{
	public override DateRangeQuery Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException("Unexpected JSON detected.");
		reader.Read();
		var fieldName = reader.GetString();
		reader.Read();
		var variant = new DateRangeQuery(fieldName);
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				var property = reader.GetString();
				if (property == "boost")
				{
					variant.Boost = JsonSerializer.Deserialize<float?>(ref reader, options);
					continue;
				}

				if (property == "format")
				{
					variant.Format = JsonSerializer.Deserialize<string?>(ref reader, options);
					continue;
				}

				if (property == "from")
				{
					variant.From = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.DateMath?>(ref reader, options);
					continue;
				}

				if (property == "gt")
				{
					variant.Gt = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.DateMath?>(ref reader, options);
					continue;
				}

				if (property == "gte")
				{
					variant.Gte = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.DateMath?>(ref reader, options);
					continue;
				}

				if (property == "lt")
				{
					variant.Lt = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.DateMath?>(ref reader, options);
					continue;
				}

				if (property == "lte")
				{
					variant.Lte = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.DateMath?>(ref reader, options);
					continue;
				}

				if (property == "_name")
				{
					variant.QueryName = JsonSerializer.Deserialize<string?>(ref reader, options);
					continue;
				}

				if (property == "relation")
				{
					variant.Relation = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.QueryDsl.RangeRelation?>(ref reader, options);
					continue;
				}

				if (property == "time_zone")
				{
					variant.TimeZone = JsonSerializer.Deserialize<string?>(ref reader, options);
					continue;
				}

				if (property == "to")
				{
					variant.To = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.DateMath?>(ref reader, options);
					continue;
				}
			}
		}

		reader.Read();
		return variant;
	}

	public override void Write(Utf8JsonWriter writer, DateRangeQuery value, JsonSerializerOptions options)
	{
		if (value.Field is null)
			throw new JsonException("Unable to serialize DateRangeQuery because the `Field` property is not set. Field name queries must include a valid field name.");
		if (!options.TryGetClientSettings(out var settings))
			throw new JsonException("Unable to retrieve client settings required to infer field.");
		writer.WriteStartObject();
		writer.WritePropertyName(settings.Inferrer.Field(value.Field));
		writer.WriteStartObject();
		if (value.Boost.HasValue)
		{
			writer.WritePropertyName("boost");
			writer.WriteNumberValue(value.Boost.Value);
		}

		if (!string.IsNullOrEmpty(value.Format))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(value.Format);
		}

		if (value.From is not null)
		{
			writer.WritePropertyName("from");
			JsonSerializer.Serialize(writer, value.From, options);
		}

		if (value.Gt is not null)
		{
			writer.WritePropertyName("gt");
			JsonSerializer.Serialize(writer, value.Gt, options);
		}

		if (value.Gte is not null)
		{
			writer.WritePropertyName("gte");
			JsonSerializer.Serialize(writer, value.Gte, options);
		}

		if (value.Lt is not null)
		{
			writer.WritePropertyName("lt");
			JsonSerializer.Serialize(writer, value.Lt, options);
		}

		if (value.Lte is not null)
		{
			writer.WritePropertyName("lte");
			JsonSerializer.Serialize(writer, value.Lte, options);
		}

		if (!string.IsNullOrEmpty(value.QueryName))
		{
			writer.WritePropertyName("_name");
			writer.WriteStringValue(value.QueryName);
		}

		if (value.Relation is not null)
		{
			writer.WritePropertyName("relation");
			JsonSerializer.Serialize(writer, value.Relation, options);
		}

		if (!string.IsNullOrEmpty(value.TimeZone))
		{
			writer.WritePropertyName("time_zone");
			writer.WriteStringValue(value.TimeZone);
		}

		if (value.To is not null)
		{
			writer.WritePropertyName("to");
			JsonSerializer.Serialize(writer, value.To, options);
		}

		writer.WriteEndObject();
		writer.WriteEndObject();
	}
}

[JsonConverter(typeof(DateRangeQueryConverter))]
public sealed partial class DateRangeQuery
{
	public DateRangeQuery(Elastic.Clients.Elasticsearch.Field field)
	{
		if (field is null)
			throw new ArgumentNullException(nameof(field));
		Field = field;
	}

	/// <summary>
	/// <para>Floating point number used to decrease or increase the relevance scores of the query.<br/>Boost values are relative to the default value of 1.0.<br/>A boost value between 0 and 1.0 decreases the relevance score.<br/>A value greater than 1.0 increases the relevance score.</para>
	/// </summary>
	public float? Boost { get; set; }
	public Elastic.Clients.Elasticsearch.Field Field { get; set; }

	/// <summary>
	/// <para>Date format used to convert `date` values in the query.</para>
	/// </summary>
	public string? Format { get; set; }
	public Elastic.Clients.Elasticsearch.DateMath? From { get; set; }

	/// <summary>
	/// <para>Greater than.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.DateMath? Gt { get; set; }

	/// <summary>
	/// <para>Greater than or equal to.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.DateMath? Gte { get; set; }

	/// <summary>
	/// <para>Less than.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.DateMath? Lt { get; set; }

	/// <summary>
	/// <para>Less than or equal to.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.DateMath? Lte { get; set; }
	public string? QueryName { get; set; }

	/// <summary>
	/// <para>Indicates how the range query matches values for `range` fields.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.QueryDsl.RangeRelation? Relation { get; set; }

	/// <summary>
	/// <para>Coordinated Universal Time (UTC) offset or IANA time zone used to convert `date` values in the query to UTC.</para>
	/// </summary>
	public string? TimeZone { get; set; }
	public Elastic.Clients.Elasticsearch.DateMath? To { get; set; }
}

public sealed partial class DateRangeQueryDescriptor<TDocument> : SerializableDescriptor<DateRangeQueryDescriptor<TDocument>>
{
	internal DateRangeQueryDescriptor(Action<DateRangeQueryDescriptor<TDocument>> configure) => configure.Invoke(this);

	public DateRangeQueryDescriptor() : base()
	{
	}

	private float? BoostValue { get; set; }
	private Elastic.Clients.Elasticsearch.Field FieldValue { get; set; }
	private string? FormatValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? FromValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? GtValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? GteValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? LtValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? LteValue { get; set; }
	private string? QueryNameValue { get; set; }
	private Elastic.Clients.Elasticsearch.QueryDsl.RangeRelation? RelationValue { get; set; }
	private string? TimeZoneValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? ToValue { get; set; }

	/// <summary>
	/// <para>Floating point number used to decrease or increase the relevance scores of the query.<br/>Boost values are relative to the default value of 1.0.<br/>A boost value between 0 and 1.0 decreases the relevance score.<br/>A value greater than 1.0 increases the relevance score.</para>
	/// </summary>
	public DateRangeQueryDescriptor<TDocument> Boost(float? boost)
	{
		BoostValue = boost;
		return Self;
	}

	public DateRangeQueryDescriptor<TDocument> Field(Elastic.Clients.Elasticsearch.Field field)
	{
		FieldValue = field;
		return Self;
	}

	public DateRangeQueryDescriptor<TDocument> Field<TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	public DateRangeQueryDescriptor<TDocument> Field(Expression<Func<TDocument, object>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>Date format used to convert `date` values in the query.</para>
	/// </summary>
	public DateRangeQueryDescriptor<TDocument> Format(string? format)
	{
		FormatValue = format;
		return Self;
	}

	public DateRangeQueryDescriptor<TDocument> From(Elastic.Clients.Elasticsearch.DateMath? from)
	{
		FromValue = from;
		return Self;
	}

	/// <summary>
	/// <para>Greater than.</para>
	/// </summary>
	public DateRangeQueryDescriptor<TDocument> Gt(Elastic.Clients.Elasticsearch.DateMath? gt)
	{
		GtValue = gt;
		return Self;
	}

	/// <summary>
	/// <para>Greater than or equal to.</para>
	/// </summary>
	public DateRangeQueryDescriptor<TDocument> Gte(Elastic.Clients.Elasticsearch.DateMath? gte)
	{
		GteValue = gte;
		return Self;
	}

	/// <summary>
	/// <para>Less than.</para>
	/// </summary>
	public DateRangeQueryDescriptor<TDocument> Lt(Elastic.Clients.Elasticsearch.DateMath? lt)
	{
		LtValue = lt;
		return Self;
	}

	/// <summary>
	/// <para>Less than or equal to.</para>
	/// </summary>
	public DateRangeQueryDescriptor<TDocument> Lte(Elastic.Clients.Elasticsearch.DateMath? lte)
	{
		LteValue = lte;
		return Self;
	}

	public DateRangeQueryDescriptor<TDocument> QueryName(string? queryName)
	{
		QueryNameValue = queryName;
		return Self;
	}

	/// <summary>
	/// <para>Indicates how the range query matches values for `range` fields.</para>
	/// </summary>
	public DateRangeQueryDescriptor<TDocument> Relation(Elastic.Clients.Elasticsearch.QueryDsl.RangeRelation? relation)
	{
		RelationValue = relation;
		return Self;
	}

	/// <summary>
	/// <para>Coordinated Universal Time (UTC) offset or IANA time zone used to convert `date` values in the query to UTC.</para>
	/// </summary>
	public DateRangeQueryDescriptor<TDocument> TimeZone(string? timeZone)
	{
		TimeZoneValue = timeZone;
		return Self;
	}

	public DateRangeQueryDescriptor<TDocument> To(Elastic.Clients.Elasticsearch.DateMath? to)
	{
		ToValue = to;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		if (FieldValue is null)
			throw new JsonException("Unable to serialize field name query descriptor with a null field. Ensure you use a suitable descriptor constructor or call the Field method, passing a non-null value for the field argument.");
		writer.WriteStartObject();
		writer.WritePropertyName(settings.Inferrer.Field(FieldValue));
		writer.WriteStartObject();
		if (BoostValue.HasValue)
		{
			writer.WritePropertyName("boost");
			writer.WriteNumberValue(BoostValue.Value);
		}

		if (!string.IsNullOrEmpty(FormatValue))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(FormatValue);
		}

		if (FromValue is not null)
		{
			writer.WritePropertyName("from");
			JsonSerializer.Serialize(writer, FromValue, options);
		}

		if (GtValue is not null)
		{
			writer.WritePropertyName("gt");
			JsonSerializer.Serialize(writer, GtValue, options);
		}

		if (GteValue is not null)
		{
			writer.WritePropertyName("gte");
			JsonSerializer.Serialize(writer, GteValue, options);
		}

		if (LtValue is not null)
		{
			writer.WritePropertyName("lt");
			JsonSerializer.Serialize(writer, LtValue, options);
		}

		if (LteValue is not null)
		{
			writer.WritePropertyName("lte");
			JsonSerializer.Serialize(writer, LteValue, options);
		}

		if (!string.IsNullOrEmpty(QueryNameValue))
		{
			writer.WritePropertyName("_name");
			writer.WriteStringValue(QueryNameValue);
		}

		if (RelationValue is not null)
		{
			writer.WritePropertyName("relation");
			JsonSerializer.Serialize(writer, RelationValue, options);
		}

		if (!string.IsNullOrEmpty(TimeZoneValue))
		{
			writer.WritePropertyName("time_zone");
			writer.WriteStringValue(TimeZoneValue);
		}

		if (ToValue is not null)
		{
			writer.WritePropertyName("to");
			JsonSerializer.Serialize(writer, ToValue, options);
		}

		writer.WriteEndObject();
		writer.WriteEndObject();
	}
}

public sealed partial class DateRangeQueryDescriptor : SerializableDescriptor<DateRangeQueryDescriptor>
{
	internal DateRangeQueryDescriptor(Action<DateRangeQueryDescriptor> configure) => configure.Invoke(this);

	public DateRangeQueryDescriptor() : base()
	{
	}

	private float? BoostValue { get; set; }
	private Elastic.Clients.Elasticsearch.Field FieldValue { get; set; }
	private string? FormatValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? FromValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? GtValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? GteValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? LtValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? LteValue { get; set; }
	private string? QueryNameValue { get; set; }
	private Elastic.Clients.Elasticsearch.QueryDsl.RangeRelation? RelationValue { get; set; }
	private string? TimeZoneValue { get; set; }
	private Elastic.Clients.Elasticsearch.DateMath? ToValue { get; set; }

	/// <summary>
	/// <para>Floating point number used to decrease or increase the relevance scores of the query.<br/>Boost values are relative to the default value of 1.0.<br/>A boost value between 0 and 1.0 decreases the relevance score.<br/>A value greater than 1.0 increases the relevance score.</para>
	/// </summary>
	public DateRangeQueryDescriptor Boost(float? boost)
	{
		BoostValue = boost;
		return Self;
	}

	public DateRangeQueryDescriptor Field(Elastic.Clients.Elasticsearch.Field field)
	{
		FieldValue = field;
		return Self;
	}

	public DateRangeQueryDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	public DateRangeQueryDescriptor Field<TDocument>(Expression<Func<TDocument, object>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>Date format used to convert `date` values in the query.</para>
	/// </summary>
	public DateRangeQueryDescriptor Format(string? format)
	{
		FormatValue = format;
		return Self;
	}

	public DateRangeQueryDescriptor From(Elastic.Clients.Elasticsearch.DateMath? from)
	{
		FromValue = from;
		return Self;
	}

	/// <summary>
	/// <para>Greater than.</para>
	/// </summary>
	public DateRangeQueryDescriptor Gt(Elastic.Clients.Elasticsearch.DateMath? gt)
	{
		GtValue = gt;
		return Self;
	}

	/// <summary>
	/// <para>Greater than or equal to.</para>
	/// </summary>
	public DateRangeQueryDescriptor Gte(Elastic.Clients.Elasticsearch.DateMath? gte)
	{
		GteValue = gte;
		return Self;
	}

	/// <summary>
	/// <para>Less than.</para>
	/// </summary>
	public DateRangeQueryDescriptor Lt(Elastic.Clients.Elasticsearch.DateMath? lt)
	{
		LtValue = lt;
		return Self;
	}

	/// <summary>
	/// <para>Less than or equal to.</para>
	/// </summary>
	public DateRangeQueryDescriptor Lte(Elastic.Clients.Elasticsearch.DateMath? lte)
	{
		LteValue = lte;
		return Self;
	}

	public DateRangeQueryDescriptor QueryName(string? queryName)
	{
		QueryNameValue = queryName;
		return Self;
	}

	/// <summary>
	/// <para>Indicates how the range query matches values for `range` fields.</para>
	/// </summary>
	public DateRangeQueryDescriptor Relation(Elastic.Clients.Elasticsearch.QueryDsl.RangeRelation? relation)
	{
		RelationValue = relation;
		return Self;
	}

	/// <summary>
	/// <para>Coordinated Universal Time (UTC) offset or IANA time zone used to convert `date` values in the query to UTC.</para>
	/// </summary>
	public DateRangeQueryDescriptor TimeZone(string? timeZone)
	{
		TimeZoneValue = timeZone;
		return Self;
	}

	public DateRangeQueryDescriptor To(Elastic.Clients.Elasticsearch.DateMath? to)
	{
		ToValue = to;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		if (FieldValue is null)
			throw new JsonException("Unable to serialize field name query descriptor with a null field. Ensure you use a suitable descriptor constructor or call the Field method, passing a non-null value for the field argument.");
		writer.WriteStartObject();
		writer.WritePropertyName(settings.Inferrer.Field(FieldValue));
		writer.WriteStartObject();
		if (BoostValue.HasValue)
		{
			writer.WritePropertyName("boost");
			writer.WriteNumberValue(BoostValue.Value);
		}

		if (!string.IsNullOrEmpty(FormatValue))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(FormatValue);
		}

		if (FromValue is not null)
		{
			writer.WritePropertyName("from");
			JsonSerializer.Serialize(writer, FromValue, options);
		}

		if (GtValue is not null)
		{
			writer.WritePropertyName("gt");
			JsonSerializer.Serialize(writer, GtValue, options);
		}

		if (GteValue is not null)
		{
			writer.WritePropertyName("gte");
			JsonSerializer.Serialize(writer, GteValue, options);
		}

		if (LtValue is not null)
		{
			writer.WritePropertyName("lt");
			JsonSerializer.Serialize(writer, LtValue, options);
		}

		if (LteValue is not null)
		{
			writer.WritePropertyName("lte");
			JsonSerializer.Serialize(writer, LteValue, options);
		}

		if (!string.IsNullOrEmpty(QueryNameValue))
		{
			writer.WritePropertyName("_name");
			writer.WriteStringValue(QueryNameValue);
		}

		if (RelationValue is not null)
		{
			writer.WritePropertyName("relation");
			JsonSerializer.Serialize(writer, RelationValue, options);
		}

		if (!string.IsNullOrEmpty(TimeZoneValue))
		{
			writer.WritePropertyName("time_zone");
			writer.WriteStringValue(TimeZoneValue);
		}

		if (ToValue is not null)
		{
			writer.WritePropertyName("to");
			JsonSerializer.Serialize(writer, ToValue, options);
		}

		writer.WriteEndObject();
		writer.WriteEndObject();
	}
}